ROOT_DIR=$(pwd)

WORK_DIR=$ROOT_DIR/RN_Work
# PRJ_DIR=$(pwd)/third_party/ohos_react_native
PREBUILD_DIR="$WORK_DIR"/rnoh_prebuild

VERSION="6.0.0.202"
RNOH_CI="Ecosystem Solution ReactNative $VERSION"
RNOH_OUT="$ROOT_DIR/$RNOH_CI"
OUT_DIR=$ROOT_DIR/Archive/out
NODE_HOME=$WORK_DIR/command-line-tools/tool/node/bin

source ./rnoh_shell/tools.sh

# 编译 release 版本的 rnoh
function assemble_release_rnoh() {
  echo "[INFO] Start to make module 'react_native_openharmony' in release mode"
  # 编译前准备
  cd "$ROOT_DIR/rnoh_shell"
  npm i
  if [ $? -ne 0 ]; then
    echo "Failed to execute [npm i] in folder: $ROOT_DIR/rnoh_shell !!!"
    return 1
  fi
  node index.js
  cd "$PRJ_DIR"/tester/harmony
  # 编译模块
  echo "[INFO] hvigorw clean"
  hvigorw clean
  # 如果不加 --no-daemon 选项，有时候会报错。命令行模式下推荐使用此参数。
  echo "[INFO] hvigorw assembleHar --mode module -p module=react_native_openharmony@default -p product=default -p buildMode=release -p debuggable=false -p pipeline=1 --no-daemon"
  hvigorw assembleHar --mode module -p module=react_native_openharmony@default -p product=default -p buildMode=release -p debuggable=false -p pipeline=1 --no-daemon
  if [ $? -ne 0 ]; then
    echo "[ERROR] Failed to make module 'react_native_openharmony' in release mode"
    return 1
  fi
  echo "[SUCCESS] Make module 'react_native_openharmony' successfully in release mode"
  # 归档产物
  cp "./react_native_openharmony/build/default/outputs/default/react_native_openharmony.har" "$RNOH_OUT/react_native_openharmony_release-$VERSION.har"
  return 0
}

# 编译字节码的 rnoh
function assemble_abc_rnoh() {
  echo "[INFO] Start to make module 'react_native_openharmony' in bytecode mode"
  cd "$PRJ_DIR"/tester/harmony
  # 编译模块
  echo "[INFO] hvigorw clean"
  hvigorw clean
  # 如果不加 --no-daemon 选项，有时候会报错。命令行模式下推荐使用此参数。
  echo "[INFO] hvigorw assembleHar --mode module -p module=react_native_openharmony@default -p product=abc -p buildMode=release -p debuggable=false -p pipeline=1 --no-daemon"
  hvigorw assembleHar --mode module -p module=react_native_openharmony@default -p product=abc -p buildMode=release -p debuggable=false -p pipeline=1 --no-daemon
  if [ $? -ne 0 ]; then
    echo "[ERROR] Failed to make module 'react_native_openharmony' in bytecode mode"
    return 1
  fi
  echo "[SUCCESS] Make module 'react_native_openharmony' successfully in bytecode mode"
  # 归档产物
  cp "./react_native_openharmony/build/abc/outputs/default/react_native_openharmony.har" "$RNOH_OUT/react_native_openharmony_abc-$VERSION.har"
  return 0
}

function run_codegen() {
  echo "[INFO] Start to run codegen"

  cd "$PRJ_DIR/tester"
  # 替换 package.json 中的依赖为本地.tgz文件，同时启用 save 选项来自动更新 package.json 中的 dependencies 条目
  # npm install "$PREBUILD_DIR/config_files/npm/rnoh-testerino-0.0.9.tgz" --save
  # npm i
  # if [ $? -ne 0 ]; then
  #   echo "Failed to execute [npm i] in folder: tester !!!"
  #   return 1
  # fi
  npm run pack_react_native_harmony_sample_package
  npm run codegen
  if [ $? -ne 0 ]; then
    echo "Failed to execute [npm run codegen] in folder: tester !!!"
    return 1
  fi
}

function compile() {
  # 配置环境变量
  export PATH="$WORK_DIR"/command-line-tools/bin:"$WORK_DIR"/command-line-tools/tool/node/bin:$PATH
  export RNOH_C_API_ARCH="1"
  echo "开始环境监测!!!"
  echo "环境变量：$PATH"
  echo "$ node -v"
  node -v
  echo "$ npm -v"
  npm -v
  npm config set strict-ssl false -g
  npm config set sslVerify false -g
  npm config set registry https://repo.huaweicloud.com/repository/npm/ -g
  echo "$ npm config ls -l"
	npm config ls -l
  echo "$ ohpm -v"
  ohpm -v
  echo "$ hvigorw -v"
  hvigorw -v
  echo "打印 npm 源"
  npm config get registry

  # 创建RNOH临时输出目录
  check_dir --path="$RNOH_OUT"

  # 打包tgz
  generate_tgz --path=react-native-harmony-cli --install
  if [[ $? -ne 0 ]]; then
    echo "Failed to execute: --path=react-native-harmony-cli --install"
    return 1
  fi
  generate_tgz --path=react-native-harmony
  if [[ $? -ne 0 ]]; then
    echo "Failed to execute: generate_tgz --path=react-native-harmony"
    return 1
  fi
  npm cache clean --force
  generate_tgz --path=react-native-harmony-sample-package
  if [[ $? -ne 0 ]]; then
    echo "Failed to execute: generate_tgz --path=react-native-harmony-sample-package"
    return 1
  fi
  echo "package-lock.json"
  cd "$PRJ_DIR/react-native-harmony-sample-package"
  ls -al
  cat "$PRJ_DIR/react-native-harmony-sample-package/package-lock.json"
  generate_tgz --path=template
  if [[ $? -ne 0 ]]; then
    echo "[警告] Failed to execute: generate_tgz --path=template ，如果当前不是 0.72.5-ohos-5.0-release 分支，须分析原因！"
    # return 1  由于 0.72.5-ohos-5.0-release 分支暂时没有 template 模块，这里对 generate_tgz 结果的要求放宽
  fi

  # 安装ohpm 依赖
  cd "$PRJ_DIR/tester/harmony"
  ohpm install

  cd "$PRJ_DIR/tester"

  npm i
  # npm run pack_inner_hvigor_plugin
  if [ $? -ne 0 ]; then
    echo "在tester目录执行npm i失败"
    return 1
  fi

  npm run dev
  if [ $? -ne 0 ]; then
    echo "在tester目录执行npm run dev失败"
    return 1
  fi

  assemble_tester
  if [ $? -ne 0 ]; then
    echo "Failed to execute assemble_tester() !!!"
    return 1
  fi

  # 打react_native_openharmony debug har包
  assemble_har --module=react_native_openharmony
  if [ $? -ne 0 ]; then
    echo "Failed to assemble react_native_openharmony.har"
    return 1
  fi

  # 打sample_package debug har包
  run_codegen
  if [ $? -ne 0 ]; then
    echo "codegen failed"
    return 1
  fi

  assemble_har --module=sample_package
  if [ $? -ne 0 ]; then
    echo "Failed to assemble sample_package.har"
    return 1
  fi

  # 打release har包
  # rm -r "$PRJ_DIR/tester/react_native_openharmony/generated/"
  assemble_release_rnoh
  if [ $? -ne 0 ]; then
    echo "Failed to execute assemble_release_rnoh() !!!"
    return 1
  fi

  # assemble_abc_rnoh
  # if [ $? -ne 0 ]; then
  #   echo "Failed to execute assemble_abc_rnoh() !!!"
  #   return 1
  # fi

  # 归档
  cd "$RNOH_OUT"
  zip -r "$RNOH_CI.zip" *.*
  mv "$RNOH_OUT/$RNOH_CI.zip" "$OUT_DIR/"

  # 清点文件
  # cd "$RNOH_OUT"
  array=("react_native_openharmony_release-*.har" "react_native_openharmony-*.har" "sample_package-*.har" "rnoh-react-native-harmony-cli-*.tgz" "rnoh-react-native-harmony-*.tgz" "react-native-harmony-sample-package-*.tgz")
  for element in "${array[@]}"; do
    # 使用 find 命令查找符合条件的文件
    found_files=$(find . -type f -name "$element")
    # 判断是否找到文件
    if [ -z "$found_files" ]; then
      return 1
    fi
  done

  return 0
}

function compile_77() {
  # 配置环境变量
  export PATH="$WORK_DIR"/command-line-tools/bin:"$WORK_DIR"/command-line-tools/node-v22.14.0-linux-x64/bin:$PATH
  export DEVECO_SDK_HOME="$WORK_DIR/command-line-tools/sdk"

  echo "开始环境监测!!!"
  echo "环境变量：$PATH"
  echo "$ node -v"
  node -v
  echo "$ npm -v"
  npm -v
  echo "$ npm config ls -l"
	npm config ls -l
  echo "$ pnpm -v"
  pnpm -v
  echo "$ ohpm -v"
  ohpm -v
  echo "$ hvigorw -v"
  hvigorw -v

  # 创建RNOH临时输出目录
  check_dir --path="$RNOH_OUT"

  # 安装前端依赖
  echo "$ cd $PRJ_DIR/.."
  cd $PRJ_DIR/..

  echo "$ pnpm init-ws"
  pnpm init-ws
  if [ $? -ne 0 ]; then
    echo "Failed to pnpm init-ws"
    return 1
  fi

  # 自动化校验
  echo "$ pnpm verify"
  pnpm verify
  if [ $? -ne 0 ]; then
    echo "Failed to pnpm verify"
    return 1
  fi

  # 打包tgz
  generate_tgz --path=react-native-harmony-cli
  if [[ $? -ne 0 ]]; then
    echo "Failed to execute: generate_tgz --path=react-native-harmony-cli"
    return 1
  fi
  generate_tgz --path=react-native-harmony
  if [[ $? -ne 0 ]]; then
    echo "Failed to execute: generate_tgz --path=react-native-harmony"
    return 1
  fi
  generate_tgz --path=template
  if [[ $? -ne 0 ]]; then
    echo "Failed to execute: generate_tgz --path=template"
    return 1
  fi

  cd "$PRJ_DIR/tester/harmony"

  # 安装ohpm依赖
  ohpm install

  # # 打react_native_openharmony debug har包
  # assemble_har --module=react_native_openharmony
  # if [ $? -ne 0 ]; then
  #   echo "Failed to assemble react_native_openharmony.har"
  #   return 1
  # fi

  # 打前端bundle
  create_bundle --command=bundle:hermes-dev
  if [ $? -ne 0 ]; then
    echo "Failed to execute create_bundle() !!!"
    return 1
  fi

  # 构建tester
  assemble_tester
  if [ $? -ne 0 ]; then
    echo "Failed to execute assemble_tester() !!!"
    return 1
  fi

  # 归档
  cp $PRJ_DIR/../pnpm-lock.yaml
  cd "$RNOH_OUT"
  zip -r "$RNOH_CI.zip" *.*
  mv "$RNOH_OUT/$RNOH_CI.zip" "$OUT_DIR/"

  # 清点文件
  # TODO

  return 0
}

if [ "$manifest_file" = "ohos_react_native_77.xml" ]; then
  echo "Performing the compilation process on the 0.77.1-rc.1-ohos branch..."
  PRJ_DIR=$(pwd)/third_party/ohos_react_native/packages
  compile_77 $@
else
  echo "正在执行master/release分支上的编译流程..."
  PRJ_DIR=$(pwd)/third_party/ohos_react_native
  compile $@
fi
exit $?
